Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Class → Create a Python Subclass

Python Class

Create a Python Subclass

Subclasses in Python

Object-Oriented Programming (OOP) is a programming paradigm that organizes code around "objects" rather than actions and data rather than logic. Key concepts include: Classes: Blueprints for creating objects. They define the attributes (data) and methods (functions) an object will have. Objects: Instances of a class. They are the concrete realizations of the blueprint. Inheritance: A mechanism where a new class (subclass or derived class) is created from an existing class (superclass or base class). The subclass inherits the attributes and methods of the superclass and can add its own or override existing ones. This promotes code reusability and reduces redundancy. Polymorphism: The ability of objects of different classes to respond to the same method call in their own specific way. Encapsulation: Bundling data and methods that operate on that data within a class, hiding internal details and protecting data integrity.

Creating Subclasses in Python

In Python, you create a subclass using parentheses after the subclass name, specifying the superclass inside:
Creating Subclasses in Python class Superclass: # Define the superclass def __init__(self, attribute1, attribute2): self.attribute1 = attribute1 self.attribute2 = attribute2 def superclass_method(self): print("This is a superclass method.") class Subclass(Superclass): # Define the subclass inheriting from Superclass def __init__(self, attribute1, attribute2, attribute3): # Call the superclass's constructor to initialize inherited attributes super().__init__(attribute1, attribute2) self.attribute3 = attribute3 # Add a new attribute specific to the subclass def subclass_method(self): print("This is a subclass method.") # Override a superclass method def superclass_method(self): print("This is an overridden superclass method in the subclass.") # Create objects super_obj = Superclass("value1", "value2") sub_obj = Subclass("value1", "value2", "value3") # Call methods super_obj.superclass_method() sub_obj.superclass_method() sub_obj.subclass_method() print(sub_obj.attribute1) print(sub_obj.attribute3)

Output

This is a superclass method. This is an overridden superclass method in the subclass. This is a subclass method. value1 value3

Multiple Inheritance Example

Python supports multiple inheritance, meaning a subclass can inherit from multiple superclasses:
Multiple Inheritance Example to understand subclass class Animal: def __init__(self, name): self.name = name def speak(self): print("Generic animal sound") class Mammal(Animal): def __init__(self, name, fur_color): super().__init__(name) self.fur_color = fur_color def speak(self): print("Mammal sound") class FlyingCreature: def fly(self): print("Flying...") class Bat(Mammal, FlyingCreature): # Inheriting from both Mammal and FlyingCreature def __init__(self, name, fur_color): Mammal.__init__(self, name, fur_color) # Explicit superclass call for clarity def speak(self): print("Bat squeak!") my_bat = Bat("Bruce", "Brown") my_bat.speak() my_bat.fly() print(my_bat.name)

Output

Bat squeak! Flying... Bruce

Method Resolution Order (MRO)

When a subclass inherits from multiple superclasses and methods have the same name, Python uses a specific order to resolve which method to call. This order is determined by the Method Resolution Order (MRO), which you can inspect using `__mro__` attribute or `help()` function. Generally, it follows a depth-first, left-to-right approach. In the `Bat` example above, `Mammal.speak()` is called before `Animal.speak()` because `Mammal` is listed first in the inheritance.

Tutorials